home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / msdos / card / source / prot.asm < prev   
Encoding:
Assembly Source File  |  1991-10-19  |  4.3 KB  |  243 lines

  1. ;*********************************************************
  2. ;*
  3. ;*    386 Protect Memory (High-Memory) Copy program
  4. ;*
  5. ;*    1991.02.14    make Ken
  6. ;*
  7. ;*********************************************************
  8.         .386p
  9.  
  10. rmcode        segment dword public use16 'CODE'
  11. rmcode        ends
  12. pmcode        segment dword public use32 'CODE'
  13. pmcode        ends
  14. incode        segment dword public use16 'CODE'
  15. incode        ends
  16. DGROUP        group    rmcode,pmcode,incode
  17.  
  18. SEL_NUL        equ    0000h
  19. SEL_PCODE    equ    0008h
  20. SEL_PDATA    equ    0010h
  21. SEL_RCODE    equ    0018h
  22. SEL_RDATA    equ    0020h
  23. SEL_ALL        equ    0028h
  24.  
  25. rmcode        segment
  26.  
  27. int_data    dw    ?
  28. int_ctl        db    ?
  29.  
  30. ss_seg        dw    ?
  31.  
  32. cpy_src        dd    ?
  33. cpy_dis        dd    ?
  34. cpy_cnt        dd    ?
  35.  
  36. rm_idtr     label    pword        ; current protected mode IDTR value
  37. rm_idtl     dw    ?        ; limit
  38. rm_idtb     dd    ?        ; linear base address
  39. rm_gdtr     label    pword        ; current protected mode GDTR value
  40. rm_gdtl     dw    ?        ; limit
  41. rm_gdtb     dd    ?        ; linear base address
  42.  
  43. pm_idtr     label    pword        ; current protected mode IDTR value
  44. pm_idtl     dw    0        ; limit
  45. pm_idtb     dd    0        ; linear base address
  46. pm_gdtr     label    pword        ; current protected mode GDTR value
  47. pm_gdtl     dw    8*6-1        ; limit
  48. pm_gdtb     dd    ?        ; linear base address
  49.  
  50. gdt_tbl        label    near
  51. gdt_tbl_null    dd    00000000h,00000000h    ; null
  52. gdt_tbl_pcode    dd    0000FFFFh,00409A00h    ; base=?,limit=64K,r/e use32
  53. gdt_tbl_pdata    dd    0000FFFFh,00409200h    ; base=?,limit=64K,r/w use32
  54. gdt_tbl_rcode    dd    0000FFFFh,00009A00h    ; base=?,limit=64K,r/e use16
  55. gdt_tbl_rdata    dd    0000FFFFh,00009200h    ; base=?,limit=64K,r/w use16
  56. gdt_tbl_all    dd    0000FFFFh,00CF9200h    ; base=0,limit= 4G,r/w use32
  57.  
  58.         assume    cs:rmcode,ds:DGROUP
  59.  
  60. nmi_disable    proc    near
  61.         push    ax
  62.         in    al,0002h
  63.         mov    ah,al
  64.         in    al,0012h
  65.         mov    int_data,ax
  66.         mov    al,0FFh
  67.         out    0002h,al
  68.         out    0012h,al
  69.         in    al,060h
  70.         mov    int_ctl,al
  71.         pop    ax
  72.         ret
  73. nmi_disable    endp
  74.  
  75. nmi_enable    proc    near
  76.         push    ax
  77.         mov    al,int_ctl
  78.         shr    al,2
  79.         and    al,07h
  80.         out    060h,al
  81.         mov    ax,int_data
  82.         out    0012h,al
  83.         mov    al,ah
  84.         out    0002h,al
  85.         pop    ax
  86.         ret
  87. nmi_enable    endp
  88.  
  89.         public    gdt_init
  90. gdt_init    proc    near
  91.         xor    edx,edx
  92.         mov    dx,cs
  93.         shl    edx,4
  94.  
  95.         mov    eax,edx
  96.         add    eax,offset gdt_tbl
  97.         mov    pm_gdtb,eax
  98.  
  99.         mov    eax,edx
  100.         mov    word ptr gdt_tbl_pcode+2,ax
  101.         mov    word ptr gdt_tbl_pdata+2,ax
  102.         mov    word ptr gdt_tbl_rcode+2,ax
  103.         mov    word ptr gdt_tbl_rdata+2,ax
  104.         shr    eax,16
  105.         mov    byte ptr gdt_tbl_pcode+4,al
  106.         mov    byte ptr gdt_tbl_pdata+4,al
  107.         mov    byte ptr gdt_tbl_rcode+4,al
  108.         mov    byte ptr gdt_tbl_rdata+4,al
  109.         mov    byte ptr gdt_tbl_pcode+7,ah
  110.         mov    byte ptr gdt_tbl_pdata+7,ah
  111.         mov    byte ptr gdt_tbl_rcode+7,ah
  112.         mov    byte ptr gdt_tbl_rdata+7,ah
  113.  
  114.         ret
  115. gdt_init    endp
  116.  
  117. to_prot        proc    near
  118.         mov    cs_seg,cs
  119.         mov    ss_seg,ss
  120.         cli
  121.         call    nmi_disable
  122.         sgdte    rm_gdtr
  123.         sidte    rm_idtr
  124.         lgdte    pm_gdtr
  125.         lidte    pm_idtr
  126.         mov    eax,cr0
  127.         or    eax,1
  128.         mov    cr0,eax
  129.         jmp    ip_flush
  130. ip_flush:    
  131.         db    0EAh            ; far jmp
  132.         dw    offset DGROUP:prot_go
  133.         dw    SEL_PCODE
  134. to_prot        endp
  135.  
  136. ret_real    proc    near
  137.         mov    eax,cr0
  138.         and    eax,0FFFFFFFEh
  139.         mov    cr0,eax
  140.         db    0EAh
  141.         dw    real_mode
  142. cs_seg        dw    ?
  143.  
  144. real_mode    label    near
  145.         mov    ax,cs
  146.         mov    ds,ax
  147.         mov    ss,ss_seg
  148.         lgdte    rm_gdtr
  149.         lidte    rm_idtr
  150.         call    nmi_enable
  151.         sti
  152.         ret
  153. ret_real    endp
  154.  
  155. ;
  156. ; in esi source addr
  157. ; in edi copy addr
  158. ; in ecx copy byte count
  159. ;
  160.         public    hmemcpy
  161. hmemcpy        proc    near
  162.         push    bx
  163.         push    ds
  164.         push    es
  165.  
  166.         mov    cpy_src,esi
  167.         mov    cpy_dis,edi
  168.         mov    cpy_cnt,ecx
  169.         call    to_prot
  170.  
  171.         pop    es
  172.         pop    ds
  173.         pop    bx
  174.         ret
  175. hmemcpy        endp
  176.  
  177. rmcode        ends
  178.  
  179. pmcode        segment
  180.         assume    cs:pmcode,fs:DGROUP
  181.  
  182. prot_go        proc    near
  183.         mov    ax,SEL_ALL
  184.         mov    ds,ax
  185.         mov    es,ax
  186.         mov    ax,SEL_PDATA
  187.         mov    fs,ax
  188.         mov    gs,ax
  189.  
  190.         cld
  191.         mov    esi,fs:cpy_src
  192.         mov    edi,fs:cpy_dis
  193.         mov    ecx,fs:cpy_cnt
  194.         shr    ecx,2
  195.         jecxz    cpy_next
  196.         rep    movsd
  197. cpy_next:
  198.         mov    ecx,fs:cpy_cnt
  199.         and    ecx,3
  200.         jecxz    non_cpy
  201.         rep    movsb
  202. non_cpy:
  203.         mov    ax,SEL_RDATA
  204.         mov    ds,ax
  205.         mov    es,ax
  206.         mov    fs,ax
  207.         mov    gs,ax
  208.         mov    ss,ax
  209.  
  210.         db    0EAh            ; far jmp
  211.         dd    offset DGROUP:ret_real
  212.         dw    SEL_RCODE
  213.  
  214. prot_go        endp
  215.  
  216. pmcode        ends
  217.  
  218. incode        segment
  219.         assume    cs:incode,ds:DGROUP
  220.  
  221.         public    end_of_adr
  222. end_of_adr    label    byte
  223.  
  224. msg_str        db    0Dh,0Ah
  225.         db    'IC Memory Card Device Driver ver1.00 for FM-TOWNS'
  226.         db    0Dh,0Ah
  227.         db    'Copyright 1991.03 Nanno-NET(Ken)'
  228.         db    0Dh,0Ah,'$'
  229.  
  230.         public    open_msg
  231. open_msg    proc    near
  232.  
  233.         mov    dx,offset DGROUP:msg_str
  234.         mov    ah,9
  235.         int    21h
  236.         ret
  237.  
  238. open_msg    endp
  239.  
  240. incode        ends
  241.  
  242.         end
  243.